﻿using MediatR;
using Traceability.App.API.Application.Command.Img;
using Traceability.App.Domain.Entities;
using Traceability.App.ErrorCode;
using Traceability.App.Infrastructure.Interface;

namespace Traceability.App.API.Application.Handler.Img
{
    /// <summary>
    /// 删除图片信息请求命令处理类
    /// </summary>
    public class DeleteImgHandler:IRequestHandler<DeleteImgCommand,ApiResult<int>>
    {
        IBaseRepository<Picture_Information> pictureRep;

        public DeleteImgHandler(IBaseRepository<Picture_Information> pictureRep)
        {
            this.pictureRep = pictureRep;
        }

        public Task<ApiResult<int>> Handle(DeleteImgCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> res = new ApiResult<int>();

                var picture = pictureRep.GetAll().FirstOrDefault(x => x.Id == request.id && x.Production_Batch == request.production_Batch && x.PictureType == request.pictureType);
                picture.IsDeleted = true;
                int r=pictureRep.UpdateAsync(picture).Result;
                if (r > 0)
                {
                    res.Msg = "删除成功";
                    res.Code = ApiEnum.Success;
                    res.Data = r;
                }
                else
                {
                    res.Msg = "删除失败";
                    res.Code = ApiEnum.Fail;
                }
                return Task.FromResult(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
